package com.meow.action.admin.visa;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.meow.util.AjaxOut;

@Controller
@RequestMapping("/admin/visa/ContentEditorAction/")
public class ContentEditorAction {
	
	private static final long serialVersionUID = -7989492009794293870L;
	
	@Autowired
	private BasicDataSource ds;
	
	private Logger logger = Logger.getLogger(ContentEditorAction.class);
	
	@RequestMapping("save.action")
	public void save(HttpServletRequest request, HttpServletResponse response, @Param("tableName")String tableName, @Param("id")int id, @Param("content")String content) {
		JSONObject obj = new JSONObject();
		try {
			Connection conn = ds.getConnection();
			try {
				String sql = "update " + tableName.toLowerCase() + " set content=? where id=?";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				
				byte[] data = content.getBytes();
				ByteArrayInputStream bi = new ByteArrayInputStream(data);
				InputStreamReader ins = new InputStreamReader(bi);
				pstmt.setCharacterStream(1, ins, data.length);
				
				pstmt.setInt(2, id);
				pstmt.execute();
				pstmt.close();
				obj.put("result", "success");
			}
			finally {
				conn.close();
			}
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result", "error");
			obj.put("info", e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	@RequestMapping("showEditor.action")
	public String showEditor(HttpServletRequest request, HttpServletResponse response, @Param("tableName")String tableName, @Param("id")int id) {
		try {
			Connection conn = ds.getConnection();
			try {
				String sql = "select content from " + tableName.toLowerCase() + " where id=?";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, id);
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()) {
					String rsContent = rs.getString("content");
					request.setAttribute("content", rsContent);
				}
				rs.close();
				pstmt.close();
			}
			finally {
				conn.close();
			}
			return "/admin/content/ContentEditor.jsp";
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return "";
	}
	
	@RequestMapping("showPreview.action")
	public String showPreview(HttpServletRequest request, HttpServletResponse response, @Param("tableName")String tableName, @Param("id")int id) {
		try {
			Connection conn = ds.getConnection();
			try {
				String sql = "select content from " + tableName.toLowerCase() + " where id=?";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, id);
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()) {
					String rsContent = rs.getString("content");
					request.setAttribute("content", rsContent);
				}
				rs.close();
				pstmt.close();
			}
			finally {
				conn.close();
			}
			return "/admin/content/ContentPreview.jsp";
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return "";
	}
}
